import {
  Entity,
  PrimaryGeneratedColumn,
  OneToMany,
  Column,
  ManyToOne,
  ManyToMany,
  TreeChildren,
  TreeParent,
  Tree,
} from 'typeorm';
import { AccessEntity } from '../access/access.entity';
import { MenuEntity } from '../menu/menu.entity';

@Entity({ name: 'route' })
@Tree('closure-table')
// @Tree("materialized-path")
// @Tree("nested-set")
export class RouteEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column('varchar')
  name: string;

  @Column('varchar')
  path: string;

  @Column('varchar')
  icon: string;

  @Column('int')
  type: number;

  @Column('datetime')
  createDate: string;

  @ManyToMany(
    type => AccessEntity,
    access => access.routes,
    { cascade: true, eager: true },
  )
  access: AccessEntity[];

  @OneToMany(
    () => MenuEntity,
    menu => menu.route,
  )
  menus: [];

  @TreeChildren({ cascade: true, })
  children: RouteEntity[];

  @TreeParent({ onDelete: 'CASCADE' })
  parent: RouteEntity;
}
